<?php
class Cart
{
	public function __construct()
	{
		if(!isset($_SESSION['cartid']))
		{
			srand();
			$_SESSION['cartid']=rand();
			$cartid=$_SESSION['cartid'];
		} else {
			$cartid=$_SESSION['cartid'];
		}
		
		$this->cartid=$cartid;
	}
	
	public function index()
	{		
		
	}
	
	public function showCart()
	{
		error_reporting(E_ALL);
		CommonFunc::loadFile("model/Cart.php");
		
		//echo $this->cartid;
	}
	
	public function addtoCart()
	{
		CommonFunc::loadFile("model/Cart.php");
		$modCart = new FrontCart();
		$request = CommonFunc::getRequest();
		$productid = $request['productid'];
		$savecartid = $request['savecartid'];
		$qty = $request['txtQty'];
		$price = $request['price'];
		
		if($request['savecartid'] == "")
		{
			$savecartid = "";
		} else {
			$savecartid = $request['savecartid'];
		}
		$modCart->addtoCart($productid, $qty, $price, $savecartid);
	}
	
	public function updateCart()
	{
		CommonFunc::loadFile("model/Cart.php");
		CommonFunc::loadFile("model/Product.php");
		$modCart = new FrontCart();
		$modProduct = new FrontProduct();
		$request = CommonFunc::getRequest();		
		$productid = $request['productid'];
		$qty_requested = $request['qty'];
		$cartid = $request['cartid'];
		$qty_available = $modProduct->validateQty($productid);
		if($qty_requested <= $qty_available['quantity'])
		{
			$modCart->updateCart($productid, $qty_requested, $cartid);
		} else {
			echo "error";
		}
	}
	
	public function delCart()
	{
		CommonFunc::loadFile("model/Cart.php");
		$request = CommonFunc::getRequest();		
		$productid = $request['productid'];
		$modCart = new FrontCart();
		$modCart->delCart($productid);
	}
	
	public function saveCart()
	{
		CommonFunc::loadFile("model/Cart.php");
		$modCart = new FrontCart();
		$request = CommonFunc::getRequest();		
		$userid = CommonFunc::currentUserId();
		$cartname = $request['cartname'];
		$cartid = $this->cartid;
		$modCart->saveCart($cartid, $userid, $cartname);			
	}
	
	public function showSaveCart()
	{
		CommonFunc::loadFile("model/Cart.php");
		$modCart = new FrontCart();		
	}
	
	public function delSaveCart()
	{
		CommonFunc::loadFile("model/Cart.php");
		$request = CommonFunc::getRequest();		
		$savecartid = $request['savecartid'];
		$modCart = new FrontCart();
		$modCart->delSaveCart($savecartid);
	}
}

?>